package com.common.base.widget

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.widget.RadioButton
import com.common.base.R

/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：20/2/2023
 *
 * 描述：可以设置图片大小的RadioButton
 *
 * 修订历史：
 *
 */
@SuppressLint("AppCompatCustomView")
class MyRadioButton @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyle: Int = 0
) :
    RadioButton(context, attrs, defStyle) {
    private var mDrawableHeight //xml文件中设置的高度
            = 0
    private var mDrawableWidth //xml文件中设置的宽度
            = 0
    var drawableLeft: Drawable? = null
    var drawableTop: Drawable? = null
    var drawableRight: Drawable? = null
    var drawableBottom: Drawable? = null

    init {
        val a = context.obtainStyledAttributes(
            attrs,
            R.styleable.MyRadioButton
        )
        isClickable = true
        val n = a.indexCount
        for (i in 0 until n) {
            val attr = a.getIndex(i)
            when (attr) {
                R.styleable.MyRadioButton_drawableHeight -> mDrawableHeight =
                    a.getDimensionPixelSize(R.styleable.MyRadioButton_drawableHeight, 50)
                R.styleable.MyRadioButton_drawableWidth -> mDrawableWidth =
                    a.getDimensionPixelSize(R.styleable.MyRadioButton_drawableWidth, 50)
                R.styleable.MyRadioButton_drawableTop -> drawableTop = a.getDrawable(attr)
                R.styleable.MyRadioButton_drawableBottom -> drawableBottom = a.getDrawable(attr)
                R.styleable.MyRadioButton_drawableRight -> drawableRight = a.getDrawable(attr)
                R.styleable.MyRadioButton_drawableLeft -> drawableLeft = a.getDrawable(attr)
            }
        }
        a.recycle()
        setCompoundDrawablesWithIntrinsicBounds(
            drawableLeft,
            drawableTop,
            drawableRight,
            drawableBottom
        )
    }

    override fun setCompoundDrawablesWithIntrinsicBounds(
        left: Drawable?,
        top: Drawable?, right: Drawable?, bottom: Drawable?
    ) {
        left?.setBounds(0, 0, mDrawableWidth, mDrawableHeight)
        right?.setBounds(0, 0, mDrawableWidth, mDrawableHeight)
        top?.setBounds(0, 0, mDrawableWidth, mDrawableHeight)
        bottom?.setBounds(0, 0, mDrawableWidth, mDrawableHeight)
        setCompoundDrawables(left, top, right, bottom)
    }

    fun setMDrawableLeft() {
        setCompoundDrawables(null, drawableTop, drawableRight, drawableBottom)
    }
}